package cn.ghx.xboot.item;

import cn.ghx.xboot.common.Result;
import cn.ghx.xboot.store.Store;
import cn.ghx.xboot.store.StoreService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {

    public static final String POST = "";
    public static final String GET_LIST = "/list";
    public static final String GET_BY_ID = "/{id}";
    public static final String DELETE = "/{id}";

    private final ItemService service;

    public ItemController(ItemService service) {
        this.service = service;
    }


    @GetMapping(GET_BY_ID)
    public Result<Item> getById(@PathVariable String id){
        return Result.success(service.detail(id));
    }

    @DeleteMapping(DELETE)
    public Result<Boolean> delete(@PathVariable String id){
        return Result.success(service.remove(id));
    }

    @PostMapping(POST)
    public Result<Boolean> save(@RequestBody Item item){
        return Result.success(service.saveItem(item));
    }

    @GetMapping(GET_LIST)
    public Result<Page<Item>> getList(
            @RequestParam(required = false) String keyword ,
            @RequestParam(required = false) Boolean enabled,
            @RequestParam(required = false,defaultValue = "1") Integer page ,
            @RequestParam(required = false,defaultValue = "10") Integer size){
        return Result.success(service.query(keyword,enabled,page,size));
    }
}
